package com.example.excel_db.validator;

import com.example.excel_db.entity.ExcelTemplate;
import com.example.excel_db.entity.ExcelTemplateField;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.regex.Pattern;

/**
 * 用户数据校验器示例
 * 用于校验用户数据导入
 */
@Slf4j
@Component
public class UserDataValidator extends AbstractExcelDataValidator {

    // 邮箱正则表达式
    private static final Pattern EMAIL_PATTERN = Pattern.compile("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$");
    
    // 手机号正则表达式（简化版）
    private static final Pattern PHONE_PATTERN = Pattern.compile("^1[3-9]\\d{9}$");

    /**
     * 构造函数
     *
     * @param template Excel模板
     * @param fields   模板字段列表
     */
    public UserDataValidator(ExcelTemplate template, List<ExcelTemplateField> fields) {
        super(template, fields);
    }

    @Override
    protected List<String> doValidateRow(Map<String, Object> rowData, int rowIndex) {
        List<String> errors = new ArrayList<>();
        
        // 校验邮箱格式
        Object email = rowData.get("email");
        if (email != null && !email.toString().trim().isEmpty()) {
            if (!EMAIL_PATTERN.matcher(email.toString()).matches()) {
                errors.add(String.format("第%d行: 邮箱格式不正确", rowIndex + 1));
            }
        }
        
        // 校验手机号格式
        Object phone = rowData.get("phone");
        if (phone != null && !phone.toString().trim().isEmpty()) {
            if (!PHONE_PATTERN.matcher(phone.toString()).matches()) {
                errors.add(String.format("第%d行: 手机号格式不正确", rowIndex + 1));
            }
        }
        
        // 校验年龄范围
        Object age = rowData.get("age");
        if (age != null && !age.toString().trim().isEmpty()) {
            try {
                int ageValue = Integer.parseInt(age.toString());
                if (ageValue < 0 || ageValue > 120) {
                    errors.add(String.format("第%d行: 年龄必须在0-120之间", rowIndex + 1));
                }
            } catch (NumberFormatException e) {
                errors.add(String.format("第%d行: 年龄必须是整数", rowIndex + 1));
            }
        }
        
        return errors;
    }

    @Override
    protected Map<Integer, List<String>> doValidateBatch(List<Map<String, Object>> dataList) {
        Map<Integer, List<String>> errorMap = new HashMap<>();
        
        // 校验邮箱唯一性
        Map<String, Integer> emailMap = new HashMap<>();
        for (int i = 0; i < dataList.size(); i++) {
            Map<String, Object> rowData = dataList.get(i);
            Object email = rowData.get("email");
            
            if (email != null && !email.toString().trim().isEmpty()) {
                String emailStr = email.toString().trim();
                if (emailMap.containsKey(emailStr)) {
                    int duplicateRowIndex = emailMap.get(emailStr);
                    String error = String.format("第%d行: 邮箱与第%d行重复", i + 1, duplicateRowIndex + 1);
                    errorMap.computeIfAbsent(i, k -> new ArrayList<>()).add(error);
                } else {
                    emailMap.put(emailStr, i);
                }
            }
        }
        
        return errorMap;
    }

    @Override
    public String getName() {
        return "user";
    }
}